package UI;

import entity.User;

import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.sql.*;
import java.util.ArrayList;
import java.util.Vector;

public class Statistics_UI {
    private JFrame jFrame;
    public Statistics_UI(JFrame jFrame){
        this.jFrame=jFrame;
    }
    private final int width=800;
    private final int height=500;
    String titles[]={"编号","姓名","住址","电话","卡号","身份证号"};
    Vector<String> title=new Vector<>();
    Vector<Vector<String>> content=new Vector<>();
    JDialog dialog=new JDialog(jFrame,"查看系统用户信息",JDialog.DEFAULT_MODALITY_TYPE);
    DefaultTableModel tableModel;
    JTable table;
    JButton modifyBtn=new JButton("修改用户数据");
    JButton deleteBtn=new JButton("删除用户");
    JOptionPane jOptionPane=new JOptionPane();;
    ModifyUserinfor_UI modifyUserinfor_ui=new ModifyUserinfor_UI(jFrame);
    //JButton backBtn=new JButton("返回");
    public void init(){

        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/project1.1", "root", "jjbone");
            String sql="select * from user";
            Statement statement = conn.createStatement();
            ResultSet resultSet = statement.executeQuery(sql);
            ArrayList<User> users=new ArrayList<User>();
            while(resultSet.next()){
                users.add(new User(resultSet.getInt(1),resultSet.getString(2),resultSet.getString(3),resultSet.getString(4),resultSet.getString(5),resultSet.getString(6)));
            }
            resultSet.close();
            statement.close();
            conn.close();
//            for (int i = 0; i < users.size(); i++) {
//                System.out.println(users.get(i));
//            }
            for (int i = 0; i < titles.length; i++) {
                title.add(titles[i]);
            }
            for (int i = 0; i < users.size(); i++) {
                Vector<String> temp=new Vector<>();
                temp.add(String.valueOf(users.get(i).getId()));
                temp.add(users.get(i).getUsername());
                temp.add(users.get(i).getAddress());
                temp.add(users.get(i).getTellnum());
                temp.add(users.get(i).getCardnum());
                temp.add(users.get(i).getUserid());
                content.add(temp);
            }
            tableModel=new DefaultTableModel(content,title);
            table=new JTable(tableModel){
                @Override
                public boolean isCellEditable(int row, int column) {
                    return false;
                }
            };
            MyBackGroudJpanel backGroudJpanel=new MyBackGroudJpanel(ImageIO.read(new File("img/page1.jpg")));
            dialog.setIconImage(ImageIO.read(new File("img/userManage.png")));
            JPanel panel =new JPanel();
            panel.add(modifyBtn);
            panel.add(deleteBtn);
            modifyBtn.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {

                    int selectedRow = table.getSelectedRow();
                    if(selectedRow==-1){
                        jOptionPane.showMessageDialog(jFrame,"没有点击修改的行","确认修改",JOptionPane.WARNING_MESSAGE);
                        return;
                    }
                    String id = (String)tableModel.getValueAt(selectedRow, 0);
                    //System.out.println(id);
                    try {
                        Class.forName("com.mysql.cj.jdbc.Driver");
                        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/project1.1", "root", "jjbone");
                        String sql="select * from user where id =?";
                        PreparedStatement pst = conn.prepareStatement(sql);
                        pst.setInt(1,Integer.valueOf(id));
                        ResultSet resultSet1 = pst.executeQuery();
                        User user=new User();
                        if(resultSet1.next()){
                            user.setId(resultSet1.getInt(1));
                            user.setUsername(resultSet1.getString(2));
                            user.setTellnum(resultSet1.getString(3));
                            user.setAddress(resultSet1.getString(4));
                            user.setCardnum(resultSet1.getString(5));
                            user.setUserid(resultSet1.getString(6));
                        }
                        resultSet1.close();
                        pst.close();
                        conn.close();
                        modifyUserinfor_ui.userJte.setText(String.valueOf(user.getId()));
                        modifyUserinfor_ui.usernameJTe.setText(user.getUsername());
                        modifyUserinfor_ui.addreddJTe.setText(user.getAddress());
                        modifyUserinfor_ui.tellJTe.setText(user.getTellnum());
                        modifyUserinfor_ui.idJTe.setText(user.getUserid());
                        modifyUserinfor_ui.cardJta.setText(user.getCardnum());
                        modifyUserinfor_ui.init();

                    } catch (ClassNotFoundException | SQLException classNotFoundException) {
                        classNotFoundException.printStackTrace();
                    }

                }
            });
            deleteBtn.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    int selectedRow = table.getSelectedRow();
                    if(selectedRow==-1){
                        jOptionPane.showMessageDialog(jFrame,"没有点击删除的行","确认删除",JOptionPane.WARNING_MESSAGE);
                        return;
                    }
                    String id = (String)tableModel.getValueAt(selectedRow, 0);
                    Connection conn = null;
                    try {
                        conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/project1.1", "root", "jjbone");
                        String sql="delete from user where id=?";
                        PreparedStatement pst = conn.prepareStatement(sql);
                        pst.setInt(1,Integer.valueOf(id));
                        int row = pst.executeUpdate();
                        if(row>0){
                            JOptionPane.showMessageDialog(jFrame,"删除","删除信息提示",JOptionPane.OK_OPTION);
                            tableModel.removeRow(selectedRow);
                        }else{
                            JOptionPane.showMessageDialog(jFrame,"删除成功","删除信息提示",JOptionPane.OK_OPTION);
                        }
                        pst.close();
                        conn.close();
                    } catch (SQLException throwables) {
                        throwables.printStackTrace();
                    }

                }
            });
            table.setSelectionBackground(Color.CYAN);
            table.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
//            TableColumn column_id = table.getColumn(titles[0]);
//            column_id.setMaxWidth(30);
//            TableColumn colum_username = table.getColumn(titles[1]);
//            colum_username.setMaxWidth(60);
//            TableColumn column_addreas = table.getColumn(titles[2]);
//            column_addreas.setMaxWidth(100);
//            TableColumn column_userid = table.getColumn(titles[5]);
//            column_userid.setMinWidth(150);
            JScrollPane jScrollPane=new JScrollPane(table);
            backGroudJpanel.add(jScrollPane);
            dialog.add(backGroudJpanel);
            dialog.add(panel,BorderLayout.SOUTH);
            dialog.setResizable(false);
            dialog.setBounds((ScreenSize.getScreenWidth()-width)/2,(ScreenSize.getScreenHeight()-height)/2,width,height);
            dialog.setVisible(true);

        } catch (IOException | ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }


    }


    public static void main(String[] args) {
        new Statistics_UI(new JFrame()).init();
    }

}


